package user

import (
	"net/http"
)

func OpenChannel(advIP string, cid int, ourb int, advb int, life int) {
	client := &http.Client{}
	var state bool

	state = InitOpenRequest(client, advIP, cid, ourb, advb, life)
	if !state {
		println("InitOpenRequest Fail: error value!")
		return
	}

	state = OpenNotifyRequest(client, cid)
	if !state {
		println("OpenNotifyRequest Fail: error value!")
		return
	}

	state = OpenAccountRequest(client, cid)
	if !state {
		println("OpenAccountRequest Fail: error value!")
		return
	}

	state = OpenUnlockRequest(client, cid)
	if !state {
		println("OpenAccountRequest Fail: error value!")
		return
	}

	state = OpenCommitRequest(client, cid)
	if !state {
		println("OpenCommitRequest Fail: error value!")
		return
	}

	state = OpenCreateRequest(client, cid)
	if !state {
		println("OpenCreateRequest Fail: error value!")
		return
	}
	println("Open channel success")
}


// value > 0 - I send to you
// value < 0 - You send to me
func UpdateChannel(cid int, value int) bool{
	client := &http.Client{}
	var state bool

	state = InitUpdateRequest(client, cid, value)
	if !state {
		println("Update Init Fail: error value!")
		return false
	}

	state = UpdateUnlockRequest(client, cid, value)
	if !state {
		println("Update Unlock Fail! Break.")
		return false
	}

	UpdateCommitRequest(client, cid)
	if !state {
		println("Update Commit Fail: error value!")
		return false
	}

	UpdateKeyExchangeRequest(client, cid, value)
	if !state {
		println("Update Key Exchange Fail: error value!")
		return false
	}

	println("update channel success")
	return true
}

func CloseChannel(cid int) {
	client := &http.Client{};
	InitCloseRequest(client, cid);
	CloseCreateRequest(client, cid);
}